package com.itheima.controller;



import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.ResultMessage;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author chenzhi
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/sysDictData")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class SysDictDataController {
    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PostMapping("/add")
    public Result add(@RequestBody SysDictData sysDictData) {
        try {
            sysDictDataService.add(sysDictData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "添加数据失败");
        }

        return new Result("success", "添加数据成功");
    }

    @PostMapping("/deleteBatch")
    public ResultMessage deleteBatch(@RequestBody SysDictData sysDictData) {
        try {
            sysDictDataService.deleteBatch(sysDictData);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(0, "error", "删除数据失败");
        }
        return new ResultMessage(1, "success", "删除数据成功");
    }

    @PostMapping("/edit")
    public ResultMessage edit(@RequestBody SysDictData sysDictData) {

        try {
            sysDictDataService.edit(sysDictData);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(0, "error", "修改数据失败");
        }
        return new ResultMessage(1, "success", "修改数据成功");
    }

    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> searchMap) {

        Page<SysDictData> pageList=sysDictDataService.getList(searchMap);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());
        return new Result("success",result) ;

    }

    @PostMapping("/getListByDictType")
    //根据字典类型获取字典数据
    public Result getListByDictType(@RequestParam("dictType") String dictType) {
        if(!dictType.equals("")) {

            String uid= sysDictTypeService.getDictTypeUid(dictType);

            List<SysDictData> sysDictDataList = sysDictDataService.getDictTypeList(uid);

            return new Result("success", sysDictDataList);
        }
        return new Result("error", "查询数据失败");
    }


    @PostMapping("/getListByDictTypeList")
    //根据字典类型数组获取字典数据
    public Result getListByDictTypeList(@RequestBody List<String> dictTypeList) {
        if (dictTypeList.size() > 0) {
            //获取到uid的数组放到集合中
            List<String> sysDictTypeUidList= sysDictTypeService.getDictTypeUidList(dictTypeList);
            List<SysDictData> sysDictDataList =new ArrayList<SysDictData>();
            for (String s : sysDictTypeUidList) {
                for (SysDictData sysDictData : sysDictDataService.getDictTypeList(s)) {
                    sysDictDataList.add(sysDictData);
                }
            }
            return new Result("success", sysDictDataList);
        }
        return new Result("error", "查询数据失败");

    }

}

